package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.customerManager.DemandAnalysis;
import com.crm.model.entity.opportunityManager.OpportunityGift;
import com.crm.model.entity.sys.UserAccount;
import com.crm.service.opportunityManager.DemandAnalysisDbService;
import com.crm.service.opportunityManager.OpportunityGiftDbService;
import com.crm.system.service.opportunityManager.DemandAnalysisService;
import com.crm.system.service.opportunityManager.OpportunityGiftService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 需求分析
 */
@Service
public class DemandAnalysisServiceImpl implements DemandAnalysisService {

    @Autowired
    private DemandAnalysisDbService demandAnalysisDbService;

    /**
     * 商机-商机详情-需求分析
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序规则
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public DhPageInfo<DemandAnalysis> demandAnalysisList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String opportunityId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<DemandAnalysis> list = demandAnalysisDbService.demandAnalysisList(opportunityId,"create_tm desc");
        PageInfo<DemandAnalysis> pageInfo = new PageInfo<DemandAnalysis>(list);
        return new DhPageInfo<DemandAnalysis>(pageInfo, list);
    }

    /**
     * 商机管理-商机详情-新增需求分析
     * @param demandAnalysis 需求分析实体
     * @return
     */
    @Override
    public boolean addDemandAnalysis(DemandAnalysis demandAnalysis) {
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
            String demandAnalysisId = CommonUtils.getGUID();
            demandAnalysis.setDemandAnalysisId(demandAnalysisId);
            demandAnalysis.setCreatePeopleId(userAccount.getAccountId());
            demandAnalysis.setCreateTm(new Date());
            demandAnalysis.setUpdatePeopleId(userAccount.getAccountId());
            demandAnalysis.setUpdateTm(new Date());
            demandAnalysis.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            Boolean result = demandAnalysisDbService.addDemandAnalysis(demandAnalysis);
            return result;

    }

    /**
     * 商机-商机详情-修改需求分析
     * @param demandAnalysisId 需求分析id
     * @return
     */
    @Override
    public DemandAnalysis getDemandAnalysisById(String demandAnalysisId) {
        return demandAnalysisDbService.getDemandAnalysisById(demandAnalysisId);
    }

    /**
     * 商机详情-修改需求分析
     * @param demandAnalysis 需求分析实体
     * @return
     */
    @Override
    public Boolean updateDemandAnalysis(DemandAnalysis demandAnalysis) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        demandAnalysis.setUpdatePeopleId(userAccount.getAccountId());
        demandAnalysis.setUpdateTm(new Date());
        Boolean result = demandAnalysisDbService.updateDemandAnalysis(demandAnalysis);
        return result;
    }

    /**
     * 商机详情-删除需求分析
     * @param demandAnalysis 需求分析实体
     * @return
     */
    @Override
    public Boolean deleteDemandAnalysis(DemandAnalysis demandAnalysis) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        demandAnalysis.setUpdatePeopleId(userAccount.getAccountId());
        demandAnalysis.setUpdateTm(new Date());
        demandAnalysis.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return demandAnalysisDbService.updateDemandAnalysis(demandAnalysis);
    }
}
